package cn.smile.business.core.impl;

import cn.smile.bean.dto.core.FileViewDTO;
import cn.smile.bean.entity.core.SwiperUser;
import cn.smile.bean.form.core.swiper.SaveSwiperForm;
import cn.smile.business.core.ISwiperUserService;
import cn.smile.commons.constant.CommonConstant;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.repository.core.SwiperUserMapper;
import cn.smile.utils.FileUtil;
import cn.smile.utils.UserUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
public class SwiperUserServiceImpl extends BaseServiceImpl<SwiperUserMapper, SwiperUser> implements ISwiperUserService {

    @Override
    public List<String> getSwiperUrlList() {
        log.info("[SwiperUserServiceImpl].[getSwiperUrlList] ------> 查询用户轮播图 URL 列表 Start");
        List<SwiperUser> baseList = baseMapper.selectList(new QueryWrapper<SwiperUser>().lambda().eq(SwiperUser::getDeleted, Boolean.FALSE).eq(SwiperUser::getUserId, UserUtil.getUserInfo().getId()));
        if (CollectionUtils.isEmpty(baseList)) {
            baseList = baseMapper.selectList(new QueryWrapper<SwiperUser>().lambda().eq(SwiperUser::getDeleted, Boolean.FALSE).eq(SwiperUser::getUserId, CommonConstant.DEFAULT_SWIPER_ID));
        }
        List<String> urlList = Lists.newArrayList();
        for (SwiperUser entity : baseList) {
            urlList.add(entity.getUrl());
        }
        log.info("[SwiperUserServiceImpl].[getSwiperUrlList] ------> 查询用户轮播图 URL 列表 End");
        return urlList;
    }

    @Override
    public List<FileViewDTO> getSwiperDetailList() {
        log.info("[SwiperUserServiceImpl].[getSwiperDetailList] ------> 查询我设置的轮播图集合 Start");
        List<FileViewDTO> dataList = Lists.newArrayList();
        List<SwiperUser> baseList = baseMapper.selectList(new QueryWrapper<SwiperUser>().lambda().eq(SwiperUser::getDeleted, Boolean.FALSE).eq(SwiperUser::getUserId, UserUtil.getUserInfo().getId()));
        if (!CollectionUtils.isEmpty(baseList)) {
            for (SwiperUser entity : baseList) {
                FileViewDTO dto = new FileViewDTO();
                dto.setUrl(entity.getUrl());
                dto.setName(FileUtil.urlToFileName(entity.getUrl()));
                dataList.add(dto);
            }
        }
        log.info("[SwiperUserServiceImpl].[getSwiperDetailList] ------> 查询我设置的轮播图集合 End");
        return dataList;
    }

    @Override
    public Boolean saveSwiper(SaveSwiperForm form) {
        log.info("[SwiperUserServiceImpl].[saveSwiper] ------> 保存轮播图 Start");
        //先删除之前的
        List<SwiperUser> baseList = baseMapper.selectList(new QueryWrapper<SwiperUser>().lambda().eq(SwiperUser::getDeleted, Boolean.FALSE).eq(SwiperUser::getUserId, UserUtil.getUserInfo().getId()));
        if (!CollectionUtils.isEmpty(baseList)) {
            for (SwiperUser entity : baseList) {
                super.delById(entity.getId());
            }
        }
        //保存现在的
        if (!ObjectUtils.isEmpty(form) && !CollectionUtils.isEmpty(form.getSwiperFiles())) {
            for (String url : form.getSwiperFiles()) {
                SwiperUser entity = this.initSwiper();
                entity.setUrl(url);
                super.create(entity);
            }
        }
        log.info("[SwiperUserServiceImpl].[saveSwiper] ------> 保存轮播图 End");
        return Boolean.TRUE;
    }

    /**
     * 初始化用户轮播图对象
     *
     * @return 轮播图对象
     */
    private SwiperUser initSwiper() {
        SwiperUser entity = new SwiperUser();
        entity.setUserId(UserUtil.getUserInfo().getId());
        return entity;
    }
}
